package com.demo.business.wx;

import com.alibaba.fastjson.JSONObject;
import com.alipay.easysdk.kms.aliyun.credentials.http.HttpRequest;
import com.demo.business.user.UserDao;
import com.demo.utils.AesCbcUtil;
import com.demo.utils.WxUtility;
import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import com.tiangong.framework.util.redis.util.Base64;
import com.tiangong.framework.util.redis.util.HttpsUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("wxlogin")
@Slf4j
public class WxService {

    @Resource
    UserDao userDao;
    private Logger logger = Logger.getLogger(this.getClass());
    public static String APPID_XCX = "wxec5d6ce83eb754a0";
    public static String APPSECRET_XCX = "80ccc5f086246447b64ef09c5079a056";

    /**
     * 描述：微信小程序授权登录</br>
     *
     * @param
     * @param
     * @return 王丹锋   2022年2月16日 下午7:55:11
     * @调用时机：
     * @业务流程：
     */
    public static void main(String[] args) {
        CDO cdoData = new CDO();
        String token = WxUtility.getAccessToken(APPID_XCX, APPSECRET_XCX);
        Return ret = WxUtility.getUserInfoFromWx("071Glw100j1cpN1Hwj400Kga622Glw1n", APPID_XCX, APPSECRET_XCX, cdoData);
        System.out.println(cdoData.toJSONString());
        System.out.println(token);
    }


    /**
     * 根据非空条件，获取数据列表
     *
     * @param cdoRequest - lUserId:用户Id
     *                   - sLoginName:
     * @return
     * @author
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return wxLoginCode(CDO cdoRequest) {
        CDO cdoData = new CDO();
        Return ret = WxUtility.getUserInfoFromWx(cdoRequest.getString("sWxAuthCode"), APPID_XCX, APPSECRET_XCX, cdoData);
        /*WxUtility.getAccessToken(APPID_XCX,APPSECRET_XCX);*/
        return ret;
    }

    /**
     * 解密用户敏感数据
     *
     * @param cdoRequest - getWxUserInfo
     *
     * @return
     * @author
     * @create: 2022-02-25 23:30:08
     */
    @OpenApi
    public Return getWxUserInfo(CDO cdoRequest) throws Exception{
        String ret = WxUtility.decrypt(cdoRequest.getString("encryptedData"), cdoRequest.getString("key"), cdoRequest.getString("vi"));
        if (ret==null){
            return Return.valueOf(201,"解密失败请核实密钥");
        }else{
            CDO cdos = new CDO();
            cdos.set("sOpenId",cdoRequest.getString("openId"));
            CDO cdouser = userDao.getUser(cdos);
            cdouser.set("sMobile",ret);
            userDao.updateUserById(cdouser);
        }
        return Return.valueOf(ret);
    }


}
